package com.ssy.lingxi.search.controller.mobile;

import com.ssy.lingxi.common.controller.BaseController;
import com.ssy.lingxi.common.model.dto.UserLoginCacheDTO;
import com.ssy.lingxi.common.response.Wrapper;
import com.ssy.lingxi.search.api.model.request.ShareCodeRequest;
import com.ssy.lingxi.search.service.ICommodityService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @menu: APP-商城公共接口
 * 商城公共接口类
 * @author jw.chen
 * @version 2.0.0
 * @date 2020/8/1
 */
@RestController
@RequestMapping("/search/mobile/shop/common")
public class MobileCommonController extends BaseController {

    @Resource
    private ICommodityService commodityService;

    /**
     * 查询商品是否上架
     * @param commodityId 商品id
     * @param type 商城类型：1.企业商城 2.积分商城 3.渠道商城 4.渠道自有商城 5.渠道积分商城
     * @param channelMemberId 渠道会员id
     */
    @RequestMapping(value = "/getCommodityIsPublish", method = RequestMethod.GET)
    public Wrapper<Boolean> getCommodityIsPublish(@RequestParam("commodityId") Long commodityId, @RequestParam("type") Integer type, @RequestParam(value = "channelMemberId", required = false) Long channelMemberId) {
        UserLoginCacheDTO sysUser = this.getSysUser();
        return Wrapper.success(commodityService.getCommodityIsPublish(commodityId, type, channelMemberId, sysUser.getMemberId()));
    }

    /**
     * 保存分享码
     * @param shareCodeRequest  参数
     */
    @RequestMapping(value = "/saveShareCode", method = RequestMethod.POST)
    public Wrapper<Boolean> saveShareCode(@RequestBody ShareCodeRequest shareCodeRequest) {
        return Wrapper.success(commodityService.saveShareCode(shareCodeRequest));
    }

    /**
     * 查询分享码
     * @param code 分享码
     */
    @RequestMapping(value = "/getShareCode", method = RequestMethod.GET)
    public Wrapper<String> getShareCode(@RequestParam("code") String code) {
        return Wrapper.success(commodityService.getShareCode(code));
    }

    /**
     * 通过商品skuId查询商品库存
     * @param commoditySkuId 商品skuId
     */
    @RequestMapping(value = "/getCommodityStockBySkuId", method = RequestMethod.GET)
    public Wrapper<Double> getCommodityStockBySkuId(@RequestParam("commoditySkuId") Long commoditySkuId) {
        Long shopId = this.getHeadersShopId();
        UserLoginCacheDTO sysUser = this.getSysUser();
        return Wrapper.success(commodityService.getCommodityStockBySkuId(shopId, sysUser.getMemberId(), commoditySkuId));
    }
}
